package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.U_cate;

import cn.com.api.service.U_cateService;

import cn.com.util.*;


public class U_cateBaseController {

	private static Logger logger = Logger.getLogger(U_cateBaseController.class);
	
	@Autowired
	private U_cateService u_cateService;
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping(value = "u_cate-list")
	public ModelAndView u_cate_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_cate_list.jsp");
		
		List<U_cate> u_cates = u_cateService.findU_cates();
		mv.addObject("u_cates", u_cates);
		
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "u_cate-add-pre")
	public ModelAndView u_cate_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_cate_add.jsp");
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			U_cate u_cate = u_cateService.findU_cateById(Integer.valueOf(id));
			mv.addObject("u_cate",u_cate);
		}
		return mv;
	}

	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "u_cate-add")
	public void u_cate_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String cate_name = request.getParameter("cate_name");
		String cate_type = request.getParameter("cate_type");
		String keywords = request.getParameter("keywords");
		String cate_desc = request.getParameter("cate_desc");
		String sort = request.getParameter("sort");
		String parent_id = request.getParameter("parent_id");
		String creater_id = request.getParameter("creater_id");
		String modified_by = request.getParameter("modified_by");
		String modified_time = request.getParameter("modified_time");
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		//add
		if(id==null || (id.trim()).equals("")){
			U_cate u_cate = new U_cate();
            /*
		    u_cate.setId(id);
		    u_cate.setCate_name(cate_name);
		    u_cate.setCate_type(cate_type);
		    u_cate.setKeywords(keywords);
		    u_cate.setCate_desc(cate_desc);
		    u_cate.setSort(sort);
		    u_cate.setParent_id(parent_id);
		    u_cate.setCreater_id(creater_id);
		    u_cate.setModified_by(modified_by);
		    u_cate.setModified_time(modified_time);
	        */
			u_cateService.insertU_cate(u_cate);
		    //
            map.put(Util.RESULT, Util.RESULT_RC_SUCCESS);
		    map.put(Util.MESSAGE, "添加成功！");
        }
		//edit
		else{
			U_cate u_cate = u_cateService.findU_cateById(Integer.valueOf(id));
	        /*
		    u_cate.setCate_name(cate_name);
		    u_cate.setCate_type(cate_type);
		    u_cate.setKeywords(keywords);
		    u_cate.setCate_desc(cate_desc);
		    u_cate.setSort(sort);
		    u_cate.setParent_id(parent_id);
		    u_cate.setCreater_id(creater_id);
		    u_cate.setModified_by(modified_by);
		    u_cate.setModified_time(modified_time);
			*/
			u_cateService.updateU_cate(u_cate);
		    //
            map.put(Util.RESULT, Util.RESULT_RC_SUCCESS);
		    map.put(Util.MESSAGE, "修改成功！");
        }
		//
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
	/**
	 *删除
	 * @return
	 */
	@RequestMapping(value = "u_cate-delete")
	public void u_cate_delete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		
		//delete
		int ret_i =	u_cateService.deleteU_cate(Integer.valueOf(id));
		
		//
		if(ret_i > 0){
			map.put(Util.RESULT, Util.RESULT_RC_SUCCESS);
			map.put(Util.MESSAGE, "删除成功！");
		}

		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
}



